﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Windows.Forms;

namespace zielonka.co.uk.extensions.system.web.ui.webcontrols
{
	//Description:	
    /*
        ComboBox cbo = new ComboBox { Width = 50 };
        cbo.Items.Add("Short");
        cbo.Items.Add("A little longer");
        cbo.Items.Add("Holy cow, this is a really, really long item. How in the world will it fit?");
        cbo.SetDropDownWidth();
     */


    public static partial class DropDownListExtensions
	{
        public enum DropDownListSelectionType
        {
            ByValue,
            ByText
        }

        /// <summary>
        /// Sets the DropDownWidth to ensure that no item's text is cut off.
        /// </summary>
        public static void SetDropDownWidth(this ComboBox comboBox)
        {
            var g = comboBox.CreateGraphics();
            var font = comboBox.Font;
            float maxWidth = 0;

            foreach (var item in comboBox.Items)
            {
                maxWidth = Math.Max(maxWidth, g.MeasureString(item.ToString(), font).Width);
            }

            if (comboBox.Items.Count > comboBox.MaxDropDownItems)
            {
                maxWidth += SystemInformation.VerticalScrollBarWidth;
            }

            comboBox.DropDownWidth = Math.Max(comboBox.Width, Convert.ToInt32(maxWidth));
        }
	}
}
